/* $Id: CacheModel.java 95 2010-10-30 22:12:55Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.models;

import javax.cache.Cache;

import org.apache.log4j.Logger;

import alt.djudge.frontend.server.datatypes.AbstractEntry;
import alt.djudge.frontend.server.datatypes.ContestLanguageEntry;
import alt.djudge.frontend.server.datatypes.ContestProblemEntry;
import alt.djudge.frontend.server.datatypes.ContestUserEntry;
import alt.djudge.frontend.server.datatypes.UserEntry;
import alt.djudge.frontend.server.datatypes.scores.ContestProblemScoreEntry;
import alt.djudge.frontend.server.datatypes.scores.ContestScoreEntry;
import alt.djudge.frontend.server.datatypes.scores.ContestUserScoreEntry;
import alt.djudge.frontend.server.datatypes.scores.ProblemUserScoreEntry;
import alt.djudge.frontend.server.datatypes.scores.UserScoreEntry;


public class CacheModel
{
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(CacheModel.class);
	
	static Cache defaultCache;

	static
	{
/*		try
		{
			if (Deployment.isGAE())
			{
				CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
				Map<Object, Object> props2 = new HashMap<Object, Object>();
				defaultCache = cacheFactory.createCache(props2);
			}
			else
			{
				net.sf.cache4j.CacheFactory cacheFactory = net.sf.cache4j.CacheFactory.getInstance();
				cacheFactory.loadConfig(new FileInputStream("/home/alt/cache4j.xml"));
				log.debug(new File(".").getAbsolutePath());
				final net.sf.cache4j.Cache c = net.sf.cache4j.CacheFactory.getInstance().getCache("default");
				defaultCache = new Cache()
				{
					
					@Override
					public Collection values()
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public int size()
					{
						return c.size();
					}
					
					@Override
					public Object remove(Object arg0)
					{
						try
						{
							c.remove(arg0);
						}
						catch (net.sf.cache4j.CacheException e)
						{
							log.warn("23", e);
						}
						return null;
					}
					
					@Override
					public void putAll(Map arg0)
					{
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public Object put(Object arg0, Object arg1)
					{
						try
						{
							c.put(arg0, arg1);
						}
						catch (net.sf.cache4j.CacheException e)
						{
							log.warn("22", e);
						}
						return null;
					}
					
					@Override
					public Set keySet()
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public boolean isEmpty()
					{
						// TODO Auto-generated method stub
						return false;
					}
					
					@Override
					public Object get(Object arg0)
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public Set entrySet()
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public boolean containsValue(Object arg0)
					{
						// TODO Auto-generated method stub
						return false;
					}
					
					@Override
					public boolean containsKey(Object arg0)
					{
						// TODO Auto-generated method stub
						return false;
					}
					
					@Override
					public void clear()
					{
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void removeListener(CacheListener arg0)
					{
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public Object peek(Object arg0)
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public void loadAll(Collection arg0)
					{
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void load(Object arg0)
					{
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public CacheStatistics getCacheStatistics()
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public CacheEntry getCacheEntry(Object arg0)
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public Map getAll(Collection arg0)
					{
						// TODO Auto-generated method stub
						return null;
					}
					
					@Override
					public void evict()
					{
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void addListener(CacheListener arg0)
					{
						// TODO Auto-generated method stub
						
					}
				};;;
				log.fatal("Cache: " + defaultCache);
			}
		}
		catch (CacheException e)
		{
			log.fatal("Cannot init cache", e);
		}
		catch (net.sf.cache4j.CacheException e)
		{
			log.fatal("Cannot init cache", e);
		}
		catch (FileNotFoundException e)
		{
			log.fatal("Cannot init cache", e);
		}*/
	}

	public static Cache getStaticCache()
	{
		return defaultCache;
	}
	
	public static final String getEntriesMapKey(Class<? extends AbstractEntry> entryClass)
	{
		return entryClass.getSimpleName() + "::Map";
	}	
	
	public static final String getEntriesAllKey(Class<? extends AbstractEntry> entryClass)
	{
		return entryClass.getSimpleName() + "::All";
	}	
	
	public static final String getEntriesAllActiveKey(Class<? extends AbstractEntry> entryClass)
	{
		return entryClass.getSimpleName() + "::AllActive";
	}	
	
	public static String getEntryByIdKey(
			Class<? extends AbstractEntry> entryClass, String entryId)
	{
		return entryClass.getSimpleName() + "#" + entryId;
	}
	
	public static final String getEntryByIdKey(Class<? extends AbstractEntry> entryClass, Long entryId)
	{
		return getEntryByIdKey(entryClass, "" + entryId);
	}
	
	public static final String getEntryBySidKey(Class<? extends AbstractEntry> entryClass, String entrySid)
	{
		return entryClass.getSimpleName() + "#sid=" + entrySid;
	}
	
	public static final String getUserUsernameKey(String username)
	{
		return UserEntry.class.getSimpleName() + "#un=" + username;
	}

	public static final String getUserEmailKey(String email)
	{
		return UserEntry.class.getSimpleName() + "#email=" + email;
	}
	
/*	public static final String getCPUSByCProblemUserKey(Long contestProblemId, Long userId)
	{
		return ProblemUserScoreEntry.class.getSimpleName() + "#cp_id=" + contestProblemId + "#user=" + userId;
	}

	public static final String getCPUSMapByContestUserKey(String contestId,
			Long userId)
	{
		return ProblemUserScoreEntry.class.getSimpleName()+ "_Set_" + "#contest=" + contestId + "#user=" + userId;
	}

	public static final String getCPUSMapByContestUserKey(Long contestId,
			Long userId)
	{
		return getCPUSMapByContestUserKey(contestId.toString(), userId);
	}*/

	public static final String getMonitorKey(Long contestId)
	{
		return getMonitorKey(contestId.toString());
	}
	
	public static final String getMonitorKey(String contestId)
	{
		return "$Monitor$#" + contestId;
	}

	public static final String getMonitorRowKey(Long contestId, Long userId)
	{
		return "$MonitorRow$" + "#contest=" + contestId + "#user=" + userId;
	}
	
	public static void clearCache()
	{
		defaultCache.clear();
	}
	
	/*            */
	
	/* User Score */
	public static String getUserScoreEntryKey(Long userId)
	{
		return getUserScoreEntryKey(userId.toString());
	}	
	
	public static String getUserScoreEntryKey(String userId)
	{
		return UserScoreEntry.class.getSimpleName() + "#user=" + userId;
	}
	
	public static String getUserScoreListKey(String contestProblemId)
	{
		return UserScoreEntry.class.getSimpleName() + "::List::#user=" + contestProblemId;
	}
	
	/* Contest Score */
	public static String getContestScoreEntryKey(Long contestId)
	{
		return getContestScoreEntryKey(contestId.toString());
	}	
	
	public static String getContestScoreEntryKey(String contestId)
	{
		return ContestScoreEntry.class.getSimpleName() + "#contest=" + contestId;
	}
	
	public static String getContestScoreListKey(Long contestId)
	{
		return getContestScoreListKey(contestId.toString());
	}	
	
	public static String getContestScoreListKey(String contestId)
	{
		return ContestScoreEntry.class.getSimpleName() + "::List::#contest=" + contestId;
	}

	/* ProblemUser */
	public static String getProblemUserScoreEntryKey(String contestProblemId, String userId)
	{
		return ProblemUserScoreEntry.class.getSimpleName() + "#problem=" + contestProblemId + "#user=" + userId;
	}

	/* ContestUser */
	public static String getContestUserScoreEntryKey(String contestId, String userId)
	{
		return ContestUserScoreEntry.class.getSimpleName() + "#contest=" + contestId + "#user=" + userId;
	}
	
	/* ContestProblem */
	public static String getContestProblemScoreEntryKey(String contestId, String contestProblemId)
	{
		return ContestProblemScoreEntry.class.getSimpleName() + "#contest=" + contestId + "#problem=" + contestProblemId;
	}
	
	public static String getContestProblemScoreEntryKey(Long contestId, Long contestProblemId)
	{
		return getContestProblemScoreEntryKey(contestId.toString(), contestProblemId.toString());
	}

	public static String getContestUserScoreEntryKey(Long contestId, Long userId)
	{
		return getContestUserScoreEntryKey(contestId.toString(), userId.toString());
	}

	public static String getProblemUserScoreEntryKey(Long contestProblemId,
			Long userId)
	{
		return getProblemUserScoreEntryKey(contestProblemId.toString(), userId.toString());
	}

	public static String getProblemUserScoreMapEntries(Long contestId, Long userId)
	{
		return ProblemUserScoreEntry.class.getSimpleName() + "#contest=" + contestId + "#user" + userId;
	}
	
	public static String getContestProblemScoreMapEntries(Long contestId)
	{
		return ContestProblemScoreEntry.class.getSimpleName() + "::Map::#contest=" + contestId;
	}

	public static String getContestProblemMapEntries(Long contestId)
	{
		return ContestProblemEntry.class.getSimpleName() + "::Map::#contest=" + contestId;
	}

	public static String getContestUserEntryKey(String contestId, String userId)
	{
		return ContestUserEntry.class.getSimpleName() + "#contest=" + contestId + "#user=" + userId;
	}

	public static String getContestLanguagesKey(String contestId)
	{
		return ContestLanguageEntry.class.getSimpleName() + "#contest=" + contestId;
	}
	
	public static String getContestProblemScoreEntriesMapKey(Long contestId)
	{
		return ContestProblemScoreEntry.class.getSimpleName() + "::Map::#contest=" + contestId;
	}
}


